package com.yequan.thread.keyword.synchronizedkey;

/**
 * @Auther: Administrator
 * @Date: 2019/9/22 18:28
 * @Description:
 */
public class ObjectLock implements Runnable {

    static ObjectLock instance = new ObjectLock();
    Object lock = new Object();

    @Override
    public void run() {
        synchronized (lock) {
            try {
                System.out.println(Thread.currentThread().getName() + ": 进入 q");
                Thread.sleep(3000);
                System.out.println(Thread.currentThread().getName() + ": 结束 q");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        synchronized (lock) {
            try {
                System.out.println(Thread.currentThread().getName() + ": 进入 w");
                Thread.sleep(3000);
                System.out.println(Thread.currentThread().getName() + ": 结束 w");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);

        t1.start();
        t2.start();

        t1.join();
        t2.join();

//        while (t1.isAlive() || t2.isAlive()) {
//        }

        System.out.println("Finished");

    }
}
